package com.yudongyang.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.yudongyang.domain.User;
import com.yudongyang.service.UserService;

@Controller
public class UserController {

	@Resource
	private UserService userService;
	
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "2")Integer pageSize) {
		PageInfo<User> info = userService.list(pageNum,pageSize);
		model.addAttribute("info", info);
		return "list";
	}
	@GetMapping("/add")
	public String add() {
		return "add";
	}
	@ResponseBody
	@PostMapping("/add")
	public boolean add(User u,MultipartFile file) {
		System.out.println(u);
		if(file != null && file.getSize()>0) {
			String path="d:\\pic";
			String fileName=UUID.randomUUID().toString()+file.getOriginalFilename();
			File file2 = new File(path, fileName);
			try {
				file.transferTo(file2);
				u.setPic(fileName);
				
				
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int i = userService.add(u);
		
		
		return i>0;
	}
}
